﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;

namespace wage.Controls
{
    public class gridWorkTime : common.controls.baseDataGridView
    {
        public enum ColumnNames
        {
            EmCode, EmName, ComeLateInMins, LeaveEarlyInMins, WorkMins, OvertimeMins
        };
        public gridWorkTime()
        {
            this.SetColorType(common.Theme.GridType.Edit);
            this.myOnNewLineAdd += new common.controls.baseDataGridView.OnNewLineAdd(OnAddNewLine);
            this.CellValueChanged += new DataGridViewCellEventHandler(OnDetailCellValueChanged);
            this.myOnFind += new OnCellEvent(OnFindData);
            this.myAutoFitColumn = ColumnNames.EmName.ToString();
        }

        #region protected
        #endregion

        #region override
        public override void Init()
        {
            base.Init();
            data.wageDS.wgWorkTimeDataTable dummyTbl = DataAccess.DataCache.myWageDS.wgWorkTime;

            // 
            // emCode
            // 
            common.controls.DataGridViewTextBoxColumnExt emCode = new common.controls.DataGridViewTextBoxColumnExt();
            emCode.Uppercase = true;
            emCode.MaxInputLength = dummyTbl.emCodeColumn.MaxLength;
            emCode.DataPropertyName = dummyTbl.emCodeColumn.ColumnName;
            emCode.HeaderText = "Mã.NV";
            emCode.Name = ColumnNames.EmCode.ToString();
            emCode.Width = 120;
            emCode.Visible = true;

            // 
            // emName
            // 
            BindingSource employeeNameSrc = new BindingSource();
            employeeNameSrc.DataSource = DataAccess.DataCache.myBaseDS.employee;
            DataGridViewComboBoxColumn emName = new System.Windows.Forms.DataGridViewComboBoxColumn();
            emName.DataPropertyName = dummyTbl.emCodeColumn.ColumnName;
            emName.DataSource = employeeNameSrc;
            emName.DisplayMember = DataAccess.DataCache.myBaseDS.employee.fullNameColumn.ColumnName;
            emName.ValueMember = DataAccess.DataCache.myBaseDS.employee.emCodeColumn.ColumnName;
            emName.HeaderText = "Họ.và.Tên";
            emName.Name = ColumnNames.EmName.ToString();
            emName.ReadOnly = true;
            emName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

            common.Theme.ColorSet csDisable = this.GetColorSetINFO();
            if (csDisable != null)
            {
                emName.DefaultCellStyle.BackColor = csDisable.BackColor;
                emName.DefaultCellStyle.ForeColor = csDisable.ForeColor;
            }
            emName.Width = 200;
            emName.Visible = true;
            // 
            // comeLateInMins
            // 
            DataGridViewTextBoxColumn comeLateInMins = new System.Windows.Forms.DataGridViewTextBoxColumn();
            comeLateInMins.DataPropertyName = dummyTbl.comeLateMinsColumn.ColumnName;
            comeLateInMins.DefaultCellStyle = application.SysLibs.CellTypeCOUNT();
            comeLateInMins.HeaderText = "Đến.trễ";
            comeLateInMins.Name = ColumnNames.ComeLateInMins.ToString();
            comeLateInMins.Width = 80;
            // 
            // leaveEarlyInMins
            // 
            DataGridViewTextBoxColumn leaveEarlyInMins = new System.Windows.Forms.DataGridViewTextBoxColumn();
            leaveEarlyInMins.DataPropertyName = dummyTbl.leaveEarlyMinsColumn.ColumnName;
            leaveEarlyInMins.DefaultCellStyle = application.SysLibs.CellTypeCOUNT();
            leaveEarlyInMins.HeaderText = "Về.sớm";
            leaveEarlyInMins.Name = ColumnNames.LeaveEarlyInMins.ToString();
            leaveEarlyInMins.Width = 80;
            leaveEarlyInMins.Visible = true;

            // 
            // workInMins
            // 
            DataGridViewTextBoxColumn workInMins = new System.Windows.Forms.DataGridViewTextBoxColumn();
            workInMins.DataPropertyName = dummyTbl.workMinsColumn.ColumnName;
            workInMins.DefaultCellStyle = application.SysLibs.CellTypeCOUNT();
            workInMins.HeaderText = "Làm.việc";
            workInMins.Name = ColumnNames.WorkMins.ToString();
            workInMins.Width = 100;
            workInMins.ReadOnly = true;

            // 
            // overtimeInMins
            // 
            DataGridViewTextBoxColumn overtimeInMins = new System.Windows.Forms.DataGridViewTextBoxColumn();
            overtimeInMins.DataPropertyName = dummyTbl.overtimeMinsColumn.ColumnName;
            overtimeInMins.DefaultCellStyle = application.SysLibs.CellTypeCOUNT();
            overtimeInMins.HeaderText = "Ngoài.giờ";
            overtimeInMins.Name = ColumnNames.OvertimeMins.ToString();
            overtimeInMins.Width = 80;
            overtimeInMins.Visible = true;

            // 
            // this
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { emCode, emName, comeLateInMins, leaveEarlyInMins, workInMins, overtimeInMins });
            this.Margin = new System.Windows.Forms.Padding(0);
            this.ScrollBars = ScrollBars.Vertical;

            this.DisableReadOnlyColumns();
        }
        public override bool CheckData()
        {
            this.myEditDataSource.EndEdit();
            return DoValidateData(OnRowValidate);
        }

        #endregion override

        private void OnRowValidate(DataGridViewRow row, common.controls.GridRowValidateEventArgs e)
        {
            if (row.DataBoundItem == null) return;

            if (this.Columns[ColumnNames.EmCode.ToString()].Visible)
            {
                if (row.Cells[ColumnNames.EmCode.ToString()].Value.ToString().Trim() == "")
                {
                    e.ErrorText.Add(String.Format(CommonTypes.language.GetErrorText("DataEmpty1"), this.Columns[ColumnNames.EmCode.ToString()].HeaderText));
                    e.HaveError = true;
                }
            }
            //=========================
            //Check data contrainsts
            //=========================

            if (this.Columns[ColumnNames.EmCode.ToString()].Visible &&
                row.Cells[ColumnNames.EmCode.ToString()].Value.ToString().Trim() != "" &&
                DataAccess.DataCache.FindEmployee(row.Cells[ColumnNames.EmCode.ToString()].Value.ToString()) == null)
            {
                e.ErrorText.Add(String.Format(CommonTypes.language.GetErrorText("DataInvalid1"), this.Columns[ColumnNames.EmCode.ToString()].HeaderText));
                e.HaveError = true;
            }
        }
        private void OnFindData(object sender, common.controls.GridEventArgs e)
        {
            if (FindData((DataGridView)sender, e.RowIndex, e.ColumnIndex)) this.myEditDataSource.EndEdit();
        }
        private bool FindData(DataGridView grid, int rowIndex, int columnIndex)
        {
            bool retVal = false;
            string findCode = grid.Rows[rowIndex].Cells[columnIndex].EditedFormattedValue.ToString(); ;
            if (grid.CurrentCell == null) return false;

            //EmCode
            if (grid.Columns[columnIndex].Name.Trim() == ColumnNames.EmCode.ToString())
            {
                employee.Libs.myEmployeeFind.Find(findCode, true);
                data.baseDS.employeeRow employeeRow = employee.Libs.myEmployeeFind.mySelectedDataRow;

                if (employeeRow != null)
                {
                    grid.Rows[rowIndex].Cells[columnIndex].Value = employeeRow.emCode;
                    retVal = true;
                }
                else grid.Rows[rowIndex].Cells[columnIndex].Value = DBNull.Value;
                this.myEditDataSource.ResetBindings(false);
            }
            return retVal;
        }

        private void OnAddNewLine(object sender, DataGridViewRow currentRow, DataGridViewRowEventArgs addedRow)
        {
            string[] saveData = new string[1];
            saveData[0] = "0";
            if (currentRow != null)
            {
                if (currentRow.Cells[ColumnNames.OvertimeMins.ToString()].Value != null &&
                    currentRow.Cells[ColumnNames.OvertimeMins.ToString()].Value.ToString().Trim() != "")
                    saveData[0] = currentRow.Cells[ColumnNames.OvertimeMins.ToString()].Value.ToString();

            }
            if (this.myEditDataSource.Current != null)
            {
                data.wageDS.wgWorkTimeRow workTimeRow = (data.wageDS.wgWorkTimeRow)((DataRowView)this.myEditDataSource.Current).Row;
                DataAccess.DataLibs.InitData(workTimeRow);
            }
            if (this.Columns[ColumnNames.OvertimeMins.ToString()].Visible)
                addedRow.Row.Cells[ColumnNames.OvertimeMins.ToString()].Value = saveData[0];
        }
        private void OnDetailCellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (this.CurrentCell == null || !this.IsValidCell(e.RowIndex, e.ColumnIndex)) return;
            try
            {
                if (this.IsOnProcessing) return;
                this.IsOnProcessing = true;

                if (this.Columns[e.ColumnIndex].Name == ColumnNames.EmCode.ToString())
                {
                    string emCode = this.CurrentRow.Cells[ColumnNames.EmCode.ToString()].Value.ToString();
                    if (String.IsNullOrEmpty(emCode) == false)
                    {
                        DataAccess.DataCache.FindEmployee(emCode); 
                    }
                    this.myEditDataSource.ResetBindings(false);
                }
                this.IsOnProcessing = false;
            }
            catch (Exception er)
            {
                this.IsOnProcessing = false;
                this.ErrorHandler(this, er);
            }
        }
    }
}
